package club.pchome.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import club.pchome.common.constant.SystemConstant;
import club.pchome.dto.Result;
import club.pchome.model.AutoSign;
import club.pchome.model.InterfaceDto;
import club.pchome.service.AutoSignService;
import club.pchome.service.InterfaceService;
import club.pchome.service.UserInfoService;
import club.pchome.util.DateUtil;
import club.pchome.util.JsoupSignAdm;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @author Watler
 * 交易专用类
 */
@Api(tags = { "交易专用接口" })
@RestController
public class TransactionController extends BaseController {

	@Autowired
	AutoSignService autoSignService;

	@Autowired
	UserInfoService userInfoService;

	@Autowired
	InterfaceService interfaceService;

	@Autowired
	JsoupSignAdm signAdm;

	private static int COUNT_NO_ORDER = 1;

	/**
	 * 按归属人查找挂买单
	 *
	 * @param request
	 * @param userName
	 * @return
	 */
	@PostMapping("/transactionByholder")
	public Result<List<Object>> transactionByholder(String userName) {
		String holder = null;
		List<AutoSign> list = autoSignService.getUserHolder(userName);
		List<Object> __list = new ArrayList<Object>();

		if (list.size() > 0) {

			for (AutoSign autoSign : list) {
				//获取cookie
				String userInfo = "{\"loginName\":\"" + autoSign.getPhone() + "\",\"pwd\":\"" + autoSign.getPwd() + "\"}";
				String JSESSIONID = signAdm.AdmLogin(userInfo);
				holder = autoSign.getPhone();
				if (JSESSIONID != null) {
					//获取用户资料数据
					String ACCOUNT_DATA_RESULT = signAdm.JsoupByHangList(JSESSIONID);
					JSONObject account_data = JSONObject.parseObject(ACCOUNT_DATA_RESULT);

					String status_code = account_data.getString("code");
					if (status_code.equals("200")) {

						String dataList = account_data.getString("data");
						JSONObject objList = JSONObject.parseObject(dataList);

						int totalCount = objList.getIntValue("totalCount");

						if (totalCount > 0) {
							JSONArray _list = objList.getJSONArray("list");
							for (int i = 0; i < totalCount; i++) {
								Map<String, String> map = new HashMap<String, String>();
								JSONObject __obj = JSONObject.parseObject(_list.get(i).toString());
								String entrustOrderNum = __obj.getString("entrustOrderNum");
								int entrustAmount = __obj.getInteger("entrustAmount");
								int entrustOrderId = __obj.getInteger("entrustOrderId");
								String phone = __obj.getString("userName");

								map.put("userName", phone);
								map.put("entrustOrderId", "" + entrustOrderId);
								map.put("entrustAmount", "" + entrustAmount);
								map.put("entrustOrderNum", entrustOrderNum);
//								System.out.println("userName :"+ phone + " , entrustOrderId:" +entrustOrderId +" , entrustAmount : " + entrustAmount + ", entrustOrderNum: " + entrustOrderNum);
								__list.add(map);
							}
						}
					}
				} else {
					Result.initFailureResult(50003, "cookie 已过期!");
				}

			}

		}
		interfaceTotal(holder, "/transactionByholder");
		return Result.initSuccessResult(__list);
	}

	/**
	 * 按手机号查挂买单
	 *
	 * @param phoneList
	 * @return
	 */
	@PostMapping("/transactionByPhone")
	public Result<List<Object>> transactionByPhone(String phoneList) {
		List<AutoSign> list = autoSignService.getUserPhone(phoneList);
		List<Object> __list = new ArrayList<Object>();

		if (list.size() > 0) {

			for (AutoSign autoSign : list) {
				//获取cookie
				String userInfo = "{\"loginName\":\"" + autoSign.getPhone() + "\",\"pwd\":\"" + autoSign.getPwd() + "\"}";
				String JSESSIONID = signAdm.AdmLogin(userInfo);

				if (JSESSIONID != null) {
					//获取用户资料数据
					String ACCOUNT_DATA_RESULT = signAdm.JsoupByHangList(JSESSIONID);
					JSONObject account_data = JSONObject.parseObject(ACCOUNT_DATA_RESULT);

					String status_code = account_data.getString("code");
					if (status_code.equals("200")) {

						String dataList = account_data.getString("data");
						JSONObject objList = JSONObject.parseObject(dataList);

						int totalCount = objList.getIntValue("totalCount");

						if (totalCount > 0) {
							JSONArray _list = objList.getJSONArray("list");
							for (int i = 0; i < totalCount; i++) {
								Map<String, String> map = new HashMap<String, String>();
								JSONObject __obj = JSONObject.parseObject(_list.get(i).toString());
								String entrustOrderNum = __obj.getString("entrustOrderNum");
								int entrustAmount = __obj.getInteger("entrustAmount");
								int entrustOrderId = __obj.getInteger("entrustOrderId");
								String phone = __obj.getString("userName");

								map.put("userName", phone);
								map.put("entrustOrderId", "" + entrustOrderId);
								map.put("entrustAmount", "" + entrustAmount);
								map.put("entrustOrderNum", entrustOrderNum);
//								System.out.println("userName :"+ phone + " , entrustOrderId:" +entrustOrderId +" , entrustAmount : " + entrustAmount + ", entrustOrderNum: " + entrustOrderNum);
								__list.add(map);
							}

						}
					}
				} else {
					Result.initFailureResult(50003, "cookie 已过期!");
				}

			}

		}
		return Result.initSuccessResult(__list);
	}

	/**
	 * 按归属人更新当日是否交易
	 *
	 * @param request
	 * @param userName
	 * @return
	 */
	@ApiOperation(value = "按归属人更新当日是否交易")
	@ApiImplicitParams({@ApiImplicitParam(name = "userName", value = "归属人", required = true)})
	@PostMapping("/sellListByholder")
	public Result<String> sellListByholder(String userName) {
		List<AutoSign> list = autoSignService.getUserHolder(userName);
		String holder = null;
		if (list.size() > 0) {

			for (AutoSign autoSign : list) {
				//获取cookie
				String JSESSIONID = ADMLOGIN(autoSign.getPhone() , autoSign.getPwd());

				holder = autoSign.getPhone();
				if (JSESSIONID.equals("10021")) {
					logger.error(autoSign.getPhone() + "PASSWORD_ERROR");
				} else {
					//获取用户资料数据
					String RESULT = signAdm.JsoupToDataByCookieAndBody(SystemConstant.sellList, "{\"pageNo\":1,\"pageSize\":2000}", JSESSIONID);
					JSONObject account_data = JSONObject.parseObject(RESULT);

					String status_code = account_data.getString("code");
					if (status_code.equals("200")) {

						String dataList = account_data.getString("data");
						JSONObject objList = JSONObject.parseObject(dataList);

						int totalCount = objList.getInteger("totalCount");

						if (totalCount > 0) {
							JSONArray jsonArray = objList.getJSONArray("list");
							//只需要取第一个值
							JSONObject result = JSONObject.parseObject(jsonArray.get(0).toString());
							String phone = result.getString("userName");
							Date createTime = result.getDate("createTime");
							Date modifyTime = result.getDate("modifyTime");
							String ct = DateUtil.FMT_FN.format(createTime);
							String mt = DateUtil.FMT_FN.format(modifyTime);

							//如果创建时间 == 当前时间 ? 今天交易过 : 今天没有交易;
							if (DateUtil.yyyyMMdd.format(createTime).equals(DateUtil.yyyyMMdd.format(new Date()))) {

								autoSign.setPhone(phone);
								autoSign.setIssell(1);
								long status = autoSignService.updateAccountInfo(autoSign);
								if (status > 0) {

								} else {
									logger.info("phone : " + phone + ", 更新失败 -- sellListByholder()");
								}

							} else {
								logger.info("phone : " + phone + ", 今天没有交易 -- sellListByholder()");
							}
						} else {
							logger.error("phone : " + autoSign.getPhone() + ", 都特么交易过!闹眼子");
						}

					}

				}

			}

		}

		interfaceTotal(holder, "/sellListByholder");
		return Result.initSuccessResult("按归属人更新当日是否交易,更新完成");
	}

	/**
	 * 根据订单号购买
	 *
	 * @param phone
	 * @param pwd
	 * @param securityCode
	 * @param entrustOrderNum
	 * @param type            : adm or ut
	 * @return
	 */
	@ApiOperation(value = "根据订单号购买")
	@ApiImplicitParams({@ApiImplicitParam(name = "phone", value = "账号(手机号)", required = true), @ApiImplicitParam(name = "pwd", value = "密码", required = true),
			@ApiImplicitParam(name = "securityCode", value = "交易密码", required = true),
			@ApiImplicitParam(name = "entrustOrderNum", value = "订单编号", required = true), @ApiImplicitParam(name = "type", value = "订单类型", example = "adm" ,  required = false)})
	@PostMapping("/buy")
	public Result<String> startBuyOrderNum(String phone, String pwd, String securityCode, String entrustOrderNum, String type) {

		String JSESSIONID = ADMLOGIN(phone , pwd);
		int coinId = 1007;

		if (type != null) {
			if(!type.equals("adm") || !type.equals("ADM")){
				coinId = 1008;
			}
		}

		String jsonData = "{\"coinId\":" + coinId + ",\"entrustOrderNum\":\"" + entrustOrderNum + "\",\"securityCode\":\"" + securityCode + "\"}";

		String responseBody = signAdm.JsoupToDataByCookieAndBody(SystemConstant.buy, jsonData, JSESSIONID);

		InterfaceDto record = new InterfaceDto();
		record.setUsename(phone);
		record.setInterfacename("/buy");
		interfaceService.insert(record);

		return Result.initSuccessResult(responseBody);
	}


	/**
	 * 挂单购买 ADM 和 UT
	 *
	 * @param hodler    归属人
	 * @param unitPrice ----- UT: 默认是1 ; ADM : 当前交易
	 * @param amount    ----- 1 : ADM ; 2 : UT
	 * @param type      1	ADM : {"coinId":"1007","amount":"20","unitPrice":0.53,"securityCode":"poiuytrew","entrustType":1}
	 *                  2	UT : {"coinId":"1008","amount":"5","unitPrice":1,"securityCode":"poiuytrew","entrustType":1}
	 * @return
	 */
	@ApiOperation(value = "根据归属人挂单购买 ADM 和 UT")
	@ApiImplicitParams({@ApiImplicitParam(name = "hodler", value = "归属人", required = true),
			@ApiImplicitParam(name = "unitPrice", value = "当前汇率", required = true),@ApiImplicitParam(name = "amount", value = "购买数量", required = true),
			@ApiImplicitParam(name = "type", value = "订单类型", required = true), @ApiImplicitParam(name = "ksye", value = "可售余额", required = false)})
	@PostMapping("/sale")
	public Result<String> autoSale(String hodler, String unitPrice, int amount, int type, int ksye) {
		if(hodler == null){
			return Result.initFailureResult(5001 , "归属人不能为空");
		}
		String jsonData = null;
		int count = 0;
		List<AutoSign> list = null;
		if (ksye == 0) {
			list = autoSignService.getUserHolder(hodler);
		} else {
			list = autoSignService.getUserHolderOrKYSE(hodler, ksye);
		}
//		List<AutoSign> list = autoSignService.getUserHolder(hodler);
		if (list.size() > 0) {
			count = list.size();
			for (AutoSign autoSign : list) {
				//获取cookie
				String JSESSIONID = ADMLOGIN(autoSign.getPhone(), autoSign.getPwd());
				if (type == 1) {
					jsonData = "{\"coinId\":\"1007\",\"amount\":\"" + amount + "\",\"unitPrice\":" + unitPrice + ",\"securityCode\":\"" + autoSign.getSecuritycode() + "\",\"entrustType\":1,\"payArea\":\"1\"}";
				} else {
					jsonData = "{\"coinId\":\"1008\",\"amount\":\"" + amount + "\",\"unitPrice\":1,\"securityCode\":\"" + autoSign.getSecuritycode() + "\",\"entrustType\":1,\"payArea\":\"1\"}";
				}

				String body = signAdm.JsoupToDataByCookieAndBody(SystemConstant.sale, jsonData, JSESSIONID);
				JSONObject result = JSONObject.parseObject(body);

				String code = result.getString("code");
				String errorCode = result.getString("errorCode");

				if (code.equals("200") && errorCode.equals("SUCCESS")) {
					//{"code":"200","errorCode":"SUCCESS","msg":"ok"}
					logger.info("手机号 : " + autoSign.getPhone() + " , 挂单 " + amount +" : "+ errorCode + "!");
				} else {
					logger.info("手机号 : " + autoSign.getPhone() + " , 挂单 " + errorCode + "!!");
					--count;
				}
			}
		}

		return Result.initSuccessResult("共有 :  " + list.size() + " 个账号 , 成功的有 :  " + count + " 个");
	}

	/**
	 * 撤单
	 *
	 * @param phone
	 * @param pwd
	 * @param entrustOrderNum 订单编号
	 * @return
	 */
	@ApiOperation(value = "撤单")
	@ApiImplicitParams({@ApiImplicitParam(name = "phone", value = "手机号", required = true),
			@ApiImplicitParam(name = "pwd", value = "密码", required = true),
			@ApiImplicitParam(name = "entrustOrderNum", value = "订单编号", required = true)})
	@PostMapping("/revoke")
	public Result<String> revoke(String phone, String pwd, String entrustOrderNum) {
		String JSESSIONID = ADMLOGIN(phone, pwd);
		String body = signAdm.JsoupToDataByCookieAndBody(SystemConstant.revoke, "{\"entrustOrderNum\":\"" + entrustOrderNum + "\"}", JSESSIONID);
		JSONObject result = JSONObject.parseObject(body);

		String code = result.getString("code");
		String errorCode = result.getString("errorCode");
		String msg = result.getString("msg");
		//{"code":"200","errorCode":"SUCCESS","msg":"ok"}
		if (code.equals("200") && errorCode.equals("SUCCESS")) {
			return Result.initSuccessResult(phone + " - 撤单 - " + msg);
		} else {
			return Result.initFailureResult(40001, "手机号 : " + phone + " , 撤单失败!!!");
		}
	}

	/**
	 * 批量撤单
	 *
	 * @param phone
	 * @param pwd
	 * @param entrustOrderNum 订单编号
	 * @return
	 */
	@ApiOperation(value = "批量撤单")
	@ApiImplicitParams({@ApiImplicitParam(name = "hodler", value = "归属人", required = true)})
	@PostMapping("/allRevoke")
	public Result<String> allRevoke(String hodler) {
		List<AutoSign> list = autoSignService.getUserHolder(hodler);
		int ERROR = 0;
		int SUCCESS = 0;
		if (list.size() > 0) {

			for (AutoSign autoSign : list) {

				String JSESSIONID = ADMLOGIN(autoSign.getPhone(), autoSign.getPwd());
				String hangList = signAdm.JsoupToDataByCookieAndBody(SystemConstant.hangList, "{\"pageNo\":1,\"pageSize\":50,\"entrustType\":1}", JSESSIONID);
				JSONObject result = JSONObject.parseObject(hangList);

				String data = result.getString("data");
				JSONObject dataList = JSONObject.parseObject(data);
				int totalCount = dataList.getInteger("totalCount");
				if (totalCount > 0) {
					JSONArray json = dataList.getJSONArray("list");
					for (int i = 0; i < json.size(); i++) {
						JSONObject object = (JSONObject) json.get(i);

						String userName = object.getString("userName");
						String entrustOrderNum = object.getString("entrustOrderNum");

						String body = signAdm.JsoupToDataByCookieAndBody(SystemConstant.revoke, "{\"entrustOrderNum\":\"" + entrustOrderNum + "\"}", JSESSIONID);
						JSONObject revokeJson = JSONObject.parseObject(body);

						String code = revokeJson.getString("code");
						String errorCode = revokeJson.getString("errorCode");
						String msg = revokeJson.getString("msg");
						//{"code":"200","errorCode":"SUCCESS","msg":"ok"}
						if (code.equals("200") && errorCode.equals("SUCCESS")) {
							SUCCESS++;
							logger.info("手机号 : " + autoSign.getPhone() + " - 撤单 - " + msg);
						} else {
							ERROR++;
							logger.error("手机号 : " + autoSign.getPhone() + " - 撤单 - " + msg);
						}
					}

				} else {
					logger.info(autoSign.getPhone()+ " : 没有挂单!");
				}

			}
		}
		return Result.initSuccessResult("撤单总结, 账户总数 : " + list.size() + " ,订单个数: " + (ERROR + SUCCESS) + " , 失败: " + ERROR + " , 成功 : " + SUCCESS);
	}


	/**
	 * 根据手机号自动放行
	 *
	 * @param phone
	 * @param pwd
	 * @param securityCode
	 * @return
	 */
	@ApiOperation(value = "根据手机号自动放行")
	@ApiImplicitParams({@ApiImplicitParam(name = "phone", value = "手机号", required = true),
			@ApiImplicitParam(name = "pwd", value = "密码", required = true),
			@ApiImplicitParam(name = "securityCode", value = "交易密码", required = true)})
	@PostMapping("/auditByPhone")
	public Result<String> auditByPhone(String phone, String pwd, String securityCode) {

		String JSESSIONID = ADMLOGIN(phone , pwd);
		String sellTaskList = signAdm.JsoupToDataByCookieAndBody(SystemConstant.sellTaskList, "{\"pageNo\":1,\"pageSize\":200}", JSESSIONID);
		JSONObject result = JSONObject.parseObject(sellTaskList);
		int code = result.getInteger("code");
		String errorCode = result.getString("errorCode");

		if (code == 200 && errorCode.equals("SUCCESS")) {
			String dataList = result.getString("data");
			JSONObject objList = JSONObject.parseObject(dataList);
			JSONArray jsonArray = objList.getJSONArray("list");

			if(jsonArray.size() > 0){
				//只需要取第一个值
				JSONObject row = JSONObject.parseObject(jsonArray.get(0).toString());
				String salesRecordId = row.getString("salesRecordId");

				//去放行
				String audit = signAdm.JsoupToDataByCookieAndBody(SystemConstant.audit, "{\"auditStatus\":2,\"salesRecordId\":\"" + salesRecordId + "\",\"securityCode\":\"" + securityCode + "\"}", JSESSIONID);
				JSONObject _result = JSONObject.parseObject(audit);
				String _code = _result.getString("code");
				String _errorCode = _result.getString("errorCode");
				String _msg = result.getString("errorCode");
				if (_code.equals("200") && _errorCode.equals("SUCCESS")) {

					interfaceTotal(phone, "/auditByPhone");
					return Result.initSuccessResult("放行-" + _msg);
				} else {
					return Result.initFailureResult(60002, "放行(2)-" + _errorCode);
				}
			}else{
				return Result.initFailureResult(60004, "没有需要放行的订单!");
			}
		} else {
			return Result.initFailureResult(60003, "放行(3)-" + errorCode);
		}
	}

	/**
	 * 自动挂单匹配
	 *
	 * @param sellphone//卖方手机号
	 * @param sellpwd//卖方登录密码
	 * @param sellsecurityCode//卖方支付密码
	 * @param buyphone                 //购买方手机
	 * @param buypwd                   //购买方登录密码
	 * @param buysecurityCode          //购买方交易密码
	 * @param amount                   //购买个数
	 * @param unitPrice//当前币价
	 * @param type                     //1为adm 2为ut
	 * @return
	 */
	@PostMapping("/autoSelltoBuy")
	public Result<String> autoSelltoBuy(String sellphone, String sellpwd, String sellsecurityCode, String buyphone, String buypwd, String buysecurityCode, int amount, String unitPrice, int type) {
		String jsonData = null;
		//买单人自动挂单
		//获取cookie
		String JSESSIONID = ADMLOGIN(buyphone, buypwd);

		if (type == 1) {
			jsonData = "{\"coinId\":\"1007\",\"amount\":\"" + amount + "\",\"unitPrice\":" + unitPrice + ",\"securityCode\":\"" + buysecurityCode + "\",\"entrustType\":1,\"payArea\":\"1\"}}";
		} else {
			jsonData = "{\"coinId\":\"1008\",\"amount\":\"" + amount + "\",\"unitPrice\":1,\"securityCode\":\"" + buysecurityCode + "\",\"entrustType\":1,\"payArea\":\"1\"}";
		}

		String body = signAdm.JsoupToDataByCookieAndBody(SystemConstant.sale, jsonData, JSESSIONID);
		JSONObject result = JSONObject.parseObject(body);

		int code = result.getInteger("code");
		String errorCode = result.getString("errorCode");
		String msg = result.getString("msg");
		if (code == 200 && errorCode.equals("SUCCESS")) {

			//查询挂的单
			String ACCOUNT_DATA_RESULT = signAdm.JsoupByHangList(JSESSIONID);
			JSONObject account_data = JSONObject.parseObject(ACCOUNT_DATA_RESULT);

			String status_code = account_data.getString("code");
			if (status_code.equals("200")) {

				String dataList = account_data.getString("data");
				JSONObject objList = JSONObject.parseObject(dataList);

				int totalCount = objList.getIntValue("totalCount");

				if (totalCount > 0) {
					JSONArray _list = objList.getJSONArray("list");
					for (int i = 0; i < totalCount; i++) {
						Map<String, String> map = new HashMap<String, String>();
						JSONObject __obj = JSONObject.parseObject(_list.get(i).toString());
						String entrustOrderNum = __obj.getString("entrustOrderNum");
						int entrustAmount = __obj.getInteger("entrustAmount");
						int entrustOrderId = __obj.getInteger("entrustOrderId");
						String phone = __obj.getString("userName");

						map.put("userName", phone);
						map.put("entrustOrderId", "" + entrustOrderId);
						map.put("entrustAmount", "" + entrustAmount);
						map.put("entrustOrderNum", entrustOrderNum);
//							System.out.println("userName :"+ phone + " , entrustOrderId:" +entrustOrderId +" , entrustAmount : " + entrustAmount + ", entrustOrderNum: " + entrustOrderNum);
						_list.add(map);
					}
					//调用根据订单匹配接口
					JSONObject jsobj = _list.getJSONObject(0);
					String ddhao = jsobj.getString("entrustOrderNum");
					JSESSIONID = ADMLOGIN(sellphone, sellpwd);

					int coinId = 1007;
					if (type != 1) coinId = 1008;

					jsonData = "{\"coinId\":" + coinId + ",\"entrustOrderNum\":\"" + ddhao + "\",\"securityCode\":\"" + sellsecurityCode + "\"}";

					String responseBody = signAdm.JsoupToDataByCookieAndBody(SystemConstant.buy, jsonData, JSESSIONID);
					InterfaceDto record = new InterfaceDto();
					record.setUsename(sellphone);
					record.setInterfacename("/autoSelltoBuy");
					interfaceService.insert(record);
					logger.info("暗箱操作: " + sellphone + " 卖给 " + buyphone + " OJBK!");

					return Result.initSuccessResult(responseBody);
				} else {
					System.out.println("查不到单快滚");
				}
			}
		} else if(code == 200122 && errorCode.equals("MAX_HANG_UP_COUNT_ERROR")) {
			return Result.initFailureResult(50004, buyphone +": "+ msg);
		}else{
			return Result.initFailureResult(5004 , "手机号 : " + buyphone + " , 挂单失败!!!");
		}

		return null;
	}


	/**
	 * 自动抢单
	 *
	 * @param phone
	 * @param pwd
	 * @param securityCode
	 * @param coinId       -- ADM: 1007 ; UT: 1008;
	 * @param payArea
	 * @param amount       购买金额
	 * @return
	 */
	@PostMapping("/rushOrder")
	@ApiOperation(value = "自动抢单")
	public Result<String> rushOrder(String phone, String pwd, String securityCode, Integer coinId, Integer payArea, Integer amount) {

		coinId = coinId == null ? 1007 : 1008;
		payArea = payArea == null ? 1 : 2;
		String JSESSIONID = ADMLOGIN(phone,pwd);

		logger.error("------------"+phone+" : rushOrder -------------------");
		String orderNum = OrderList(JSESSIONID, amount, coinId, payArea);
		logger.error("------------ 用时 "+(COUNT_NO_ORDER-1)*3+"s ,抢到订单 -------------------");
		COUNT_NO_ORDER = 1;
		//去付款
		String jsonData = "{\"coinId\":" + coinId + ",\"entrustOrderNum\":\"" + orderNum + "\",\"securityCode\":\"" + securityCode + "\"}";
		String responseBody = signAdm.JsoupToDataByCookieAndBody(SystemConstant.buy, jsonData, JSESSIONID);
		return Result.initSuccessResult(responseBody);
	}


	/**
	 * 获取订单列表--大厅
	 *
	 * @param JSESSIONID
	 * @return
	 */
	public String OrderList(String JSESSIONID, Integer amount, Integer coinId, Integer payArea) {
		String responseBody = signAdm.JsoupToDataByCookieAndBody(SystemConstant.buyInList, "{\"pageNo\":1,\"pageSize\":50,\"coinId\":" + coinId + ",\"entrustType\":1,\"payArea\":" + payArea + "}", JSESSIONID);
		JSONObject account_data = JSONObject.parseObject(responseBody);

		int code = account_data.getInteger("code");
		if (code == 200) {
			String dataList = account_data.getString("data");
			JSONObject list = JSONObject.parseObject(dataList);
			JSONArray _list = list.getJSONArray("list");
			for (Object object : _list) {
				JSONObject __obj = (JSONObject) object;

				if (__obj.getIntValue("entrustAmount") == amount) {
					return __obj.getString("entrustOrderNum");
				}
			}

		} else {
			logger.error("订单获取失败!!!");
		}
		try {
			logger.error("3秒后尝试第 "+ COUNT_NO_ORDER++ +" 次重新获取!");
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		logger.error("当前没有金额为 " + amount + " 的订单!");
		return OrderList(JSESSIONID, amount, coinId, payArea);
	}


	/**
	 * 测试大厅数据
	 * @param phone
	 * @param pwd
	 * @return
	 */
	@PostMapping("/getPhoneList")
	public String getPhone(String phone, String pwd) {
		String JSESSIONID = ADMLOGIN(phone, pwd);


		try {
			String cookie = signAdm.httpGetHeader("https://adm.leange.cn/member-api/api/mem/trans/buyInList" , JSESSIONID , "cookie");
			System.out.println(cookie);
		} catch (IOException e) {
			e.printStackTrace();
		}


		String responseBody = signAdm.JsoupToDataByCookieAndBody("https://adm.leange.cn/member-api/api/mem/trans/buyInList", "{\"pageNo\":1,\"pageSize\":50,\"coinId\":1008,\"entrustType\":1,\"payArea\":1}", JSESSIONID);
		JSONObject account_data = JSONObject.parseObject(responseBody);

		int code = account_data.getInteger("code");
		if (code == 200) {
			String dataList = account_data.getString("data");
			JSONObject list = JSONObject.parseObject(dataList);

			System.out.println("共有 " + list.getIntValue("totalCount") + " 个订单");
			List<Object> _list = list.getJSONArray("list");
			int num = 0;
			for (Object object : _list) {

				JSONObject __obj = (JSONObject) object;

				String userName = __obj.getString("userName");

				System.out.println("==============================================");
				int coinId = __obj.getIntValue("coinId");
				System.out.println(getCoinIdName(coinId) + ": " + coinId);
				String Time1 = __obj.getString("createTime");
				String Time2 = __obj.getString("modifyTime");

				long createTime =  Long.valueOf(Time1);
				long modifyTime =  Long.valueOf(Time2);

				System.out.println("创建时间 : " + DateUtil.FMT_FN.format(new Date(createTime)));
				System.out.println("修改时间 : " + DateUtil.FMT_FN.format(new Date(modifyTime)));
				System.out.println("求购用户: " + userName);
				System.out.println("求购级别: " + __obj.getString("roleName"));
				System.out.println("汇率: " + __obj.getString("unitPrice"));
				System.out.println("求购数量: " + __obj.getString("entrustAmount"));
				System.out.println("求购订单: " + __obj.getString("entrustOrderId"));

				System.out.println("目前的数量: " + __obj.getString("currentVolume"));
				System.out.println("委托状态: " + __obj.getString("entrustStatus"));
				System.out.println("委托类型: " + __obj.getString("entrustType"));
				System.out.println("不知道什么鬼子费率: " + __obj.getString("rate"));

				System.out.println("可售余额: " + __obj.getDoubleValue("ksyeAmount"));
				System.out.println("可售额度: " + __obj.getDoubleValue("ksedAmount"));
				System.out.println("订单识别码: " + __obj.getString("entrustOrderNum"));

			}

		}
		return null;
	}

}
